package com.aspose.cells.b.a;

import java.math.BigDecimal;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Arrays;
import java.util.Locale;

public class zn {
   private static final double[] a = new double[]{1.0D, 10.0D, 100.0D, 1000.0D, 10000.0D, 100000.0D, 1000000.0D, 1.0E7D, 1.0E8D, 1.0E9D, 1.0E10D, 1.0E11D, 1.0E12D, 1.0E13D, 1.0E14D, 1.0E15D, 1.0E16D, 1.0E17D, 1.0E18D, 1.0E19D};

   public static int a(double var0) {
      if (var0 < 0.0D) {
         var0 = -var0;
      }

      int var2 = Arrays.binarySearch(a, var0);
      return var2 > -1 ? var2 + 1 : -var2 - 1;
   }

   public static double a(String var0) {
      return a(var0, Locale.getDefault());
   }

   public static double a(String var0, com.aspose.cells.b.a.c.za var1) {
      if (var1.c()) {
         return Double.parseDouble(var0);
      } else {
         Double var2 = a(var0, var1.h());
         if (var2 == null) {
            throw new NumberFormatException();
         } else {
            return var2;
         }
      }
   }

   public static String b(double var0) {
      if ((double)((long)var0) == var0) {
         return Long.toString((long)var0);
      } else {
         MathContext var3 = new MathContext(15);
         BigDecimal var4 = new BigDecimal(var0, var3);
         BigDecimal var5 = var4.stripTrailingZeros();
         String var2;
         if (var4.toString().contains("E")) {
            var2 = var5.toString();
         } else {
            var2 = var5.toPlainString();
            if (var2.startsWith("0.0000") || var2.startsWith("-0.0000")) {
               var2 = Double.toString(var0);
            }
         }

         if (var2.indexOf(46) > -1) {
            char var6 = com.aspose.cells.b.a.c.za.a().d().c().charAt(0);
            if (var6 != '.') {
               var2 = var2.replace('.', var6);
            }
         }

         return var2;
      }
   }

   public static String a(double var0, String var2) {
      DecimalFormat var3 = new DecimalFormat(var2);
      return a(var0, var3, var2);
   }

   public static int a(double var0, double var2) {
      if (Double.isNaN(var0) && Double.isNaN(var2)) {
         return 0;
      } else if (Double.isNaN(var0)) {
         return -1;
      } else if (Double.isNaN(var2)) {
         return 1;
      } else if (var0 == var2) {
         return 0;
      } else {
         return var0 < var2 ? -1 : 1;
      }
   }

   private static Double a(String var0, Locale var1) {
      if (var0 != null && !var0.equals("")) {
         var0 = c(var0);
         NumberFormat var2 = NumberFormat.getInstance(var1);
         int var3 = b(var0);
         if (var3 < 0) {
            var3 = 0;
         }

         ParsePosition var4 = new ParsePosition(var3);
         Number var5 = var2.parse(var0, var4);
         if (var5 == null) {
            throw new IllegalStateException("Parse double exception: illegal string format");
         } else {
            double var6 = var5.doubleValue();
            if (Double.isNaN(var6)) {
               return null;
            } else {
               var3 = var4.getIndex();
               if (var3 == var0.length() - 1 && var0.charAt(var3) == '%') {
                  return var6 / 100.0D;
               } else if (var3 < var0.length()) {
                  throw new IllegalStateException("Parse double exception: illegal string format");
               } else {
                  return var6;
               }
            }
         }
      } else {
         return null;
      }
   }

   private static int b(String var0) {
      if (var0 != null && !var0.equals("")) {
         return a(var0.charAt(0)) ? 1 : -1;
      } else {
         return -1;
      }
   }

   private static boolean a(char var0) {
      switch(var0) {
      case '$':
      case '€':
      case '￡':
      case '￥':
         return true;
      default:
         return false;
      }
   }

   private static String c(String var0) {
      var0 = var0.trim();
      if (var0.length() < 1) {
         return var0;
      } else {
         if (var0.charAt(0) == '+') {
            var0 = var0.substring(1);
         }

         var0 = var0.replace('e', 'E');
         int var1 = var0.indexOf(69);
         if (var1 > 0) {
            ++var1;
            if (var0.length() > var1 && var0.charAt(var1) == '+') {
               var0 = var0.substring(0, var1) + var0.substring(var1 + 1);
            }
         }

         return var0;
      }
   }

   private static final String a(double var0, DecimalFormat var2, String var3) {
      if (var3 == null) {
         var3 = var2.toPattern();
      }

      boolean var4 = false;
      if (var0 < 9.223372036854776E18D && (var3 == null || var3.indexOf(69) < 0) && var0 != (double)((long)var0)) {
         boolean var5 = var0 < 0.0D;
         if (var5) {
            var0 = -var0;
         }

         int var6 = a(var0);
         if (var6 < 15) {
            int var7 = var2.getMaximumFractionDigits();
            var7 = Math.min(var7, 15 - var6);
            if (var3 != null && var3.indexOf("%") != -1) {
               var7 += 2;
            }

            double var8 = a[var7 + 1];
            long var10 = (long)(var0 * var8);
            if (var10 < 10L) {
               var4 = true;
            }

            if (var10 % 10L > 4L) {
               var10 += 10L;
            }

            var10 -= var10 % 10L;
            double var12 = (double)var10 / var8;
            var8 /= 10.0D;
            var0 = (double)Math.round(var0 * var8) / var8;
            if (var12 > var0) {
               var0 = var12;
            }
         }

         if (var5) {
            var0 = -var0;
         }
      }

      String var14 = var2.format(var0);
      return d(var14);
   }

   private static final String d(String var0) {
      if (var0.length() > 0 && var0.charAt(0) == '-') {
         for(int var1 = 1; var1 < var0.length(); ++var1) {
            char var2 = var0.charAt(var1);
            if (var2 != '0' && var2 != '.') {
               return var0;
            }
         }

         return var0.substring(1);
      } else {
         return var0;
      }
   }
}
